﻿using System;
using System.Web;
using Bintje;

public partial class UI_add_trailComments : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (this.Page.User.Identity.Name == null || this.Page.User.Identity.Name == "")
            {
                Response.Redirect("../login.aspx?Retuenurl=" + Request.Url.ToString());
            }
            string captchaCode = (Guid.NewGuid().ToString().ToUpper()).Replace("o", "5").Replace("0", "").Substring(0, 5);
            imgCaptcha.ImageUrl = "~/sitefiles/captcha.aspx?code=" + captchaCode + "&w=140&h=40&fsize=20";

            ViewState["captchaCode"] = captchaCode;

            int trailId = 0;
            int.TryParse(Request["TrailId"],out trailId);

            if (trailId > 0)
            {
                Bintje.Trail trail = Bintje.Trail.Get(trailId);
                if (trail != null)
                {
                    lblTrail.Text = trail.Title;//trl[0].Name;
                    lblTrail.NavigateUrl = "~/UI/trail-details.aspx?TrailID=" + trailId.ToString();
                }

            }
            

        }
    }

    protected void btnSubmit_click(object sender, EventArgs e)
    {
        if (Request["TrailId"] != null)
        {
            if (txtCaptcha.Text.Trim().ToUpper() == (string)ViewState["captchaCode"])
            {
                int trailId = 0;
                int.TryParse(Request["TrailId"],out trailId);

                if (trailId > 0)
                {
                    Bintje.Trail trail = Bintje.Trail.Get(trailId);
                    
                    if (null != trail)
                    {
                        Annotation annotation = new Annotation();
                        annotation.Comment = txtComments.Text.Trim();
                        annotation.Created = DateTime.Now.ToBinary();
                        
                        Bintje.bintje.imageoSession session = HttpContext.Current.Session["session"] as Bintje.bintje.imageoSession;
                        if (null != session)
                        {
                            User user = new User(session);
                            if (null != user)
                            {
                                user.LoadDetails();
                                annotation.Email = user.Login;
                                annotation.UserId = user.Id;
                            }
                        }
                        trail = trail.KeepImages(trail);
                        trail.Update();
                        lblMsg.Text = "<br><b>Your comments successfully saved.</b>";
                        Response.Redirect(string.Format("~/UI/trail-details.aspx?TrailId={0}&tmsg=1", trailId.ToString()));
                    }
                }
            }
            else
            {
                lblMsg.Text = "Your typed text doesn't match the text of the image";
            }
        }
    }
}
